package com.kouxin.appaccount.misc;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

import org.aspectj.lang.JoinPoint;

public class DaoAspect {

	public void beforeInsert(JoinPoint joinPoint) {  
		Object[] args = joinPoint.getArgs();
		for (int i=0;i<args.length;i++) {
			Object obj = args[i];
			Date now = new Date();
			try {
				Method method = obj.getClass().getDeclaredMethod("setCreatedStamp", Date.class);
				if (method != null) {
					try {method.invoke(obj, now);} catch (IllegalArgumentException e) {} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {}
				}
			} catch (SecurityException e) {} catch (NoSuchMethodException e) {}
			try {
				Method method = obj.getClass().getDeclaredMethod("setCreatedTxStamp", Date.class);
				if (method != null) {
					try {method.invoke(obj, now);} catch (IllegalArgumentException e) {} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {}
				}
			} catch (SecurityException e) {} catch (NoSuchMethodException e) {}
			try {
				Method method = obj.getClass().getDeclaredMethod("setLastUpdatedStamp", Date.class);
				if (method != null) {
					try {method.invoke(obj, now);} catch (IllegalArgumentException e) {} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {}
				}
			} catch (SecurityException e) {} catch (NoSuchMethodException e) {}
			try {
				Method method = obj.getClass().getDeclaredMethod("setLastUpdatedTxStamp", Date.class);
				if (method != null) {
					try {method.invoke(obj, now);} catch (IllegalArgumentException e) {} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {}
				}
			} catch (SecurityException e) {} catch (NoSuchMethodException e) {}
		}
	}
	
	public void beforeUpdate(JoinPoint joinPoint) {  
		Object[] args = joinPoint.getArgs();
		for (int i=0;i<args.length;i++) {
			Object obj = args[i];
			Date now = new Date();
			try {
				Method method = obj.getClass().getDeclaredMethod("setLastUpdatedStamp", Date.class);
				if (method != null) {
					try {method.invoke(obj, now);} catch (IllegalArgumentException e) {} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {}
				}
			} catch (SecurityException e) {} catch (NoSuchMethodException e) {}
			try {
				Method method = obj.getClass().getDeclaredMethod("setLastUpdatedTxStamp", Date.class);
				if (method != null) {
					try {method.invoke(obj, now);} catch (IllegalArgumentException e) {} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {}
				}
			} catch (SecurityException e) {} catch (NoSuchMethodException e) {}
		}
	}
}
